/*******************************************************************************
 * Copyright (c) 2008-2009 zhang yuexiang.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.xfeep.alaya.jta.test;


import java.sql.SQLException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xfeep.alaya.jta.SmartConnection;
import org.xfeep.alaya.jta.WeaverService;
import org.xfeep.asura.bootstrap.Asura;
import org.xfeep.asura.bootstrap.MemoryComponentLoader;
import org.xfeep.asura.bootstrap.XmlComponentLoader;
import org.xfeep.asura.bootstrap.config.XmlMultipleConfigLoader;
import org.xfeep.asura.core.console.CommandLineService;

import static org.junit.Assert.*;

public class DeclaredTransactionAndSmartConnectionTestSet {


	Asura asura;
	
//	@BeforeClass
//	public static void setUpBeforeClass() throws Exception {
//		System.setProperty("com.atomikos.icatch.file", "jta.properties");
//	}
//
//	@AfterClass
//	public static void tearDownAfterClass() throws Exception {
//	}
	
	
	@Test
	public void testAllSmartConnectionTestClient() {
		asura.addComponentsFormLoaders(new MemoryComponentLoader(CommandLineService.class, SmartConnection.class));
		asura.addComponentsFormLoaders(new MemoryComponentLoader(SmartConnectionTestClient.class));
		SmartConnectionTestClient sct = asura.getApplicationServiceSpace().findService(SmartConnectionTestClient.class);
		long start = System.currentTimeMillis();
		assertNotNull(sct);
		try {
			sct.i1();
			assertEquals("i1", sct.find("i1"));
		} catch (SQLException e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		try {
			sct.i2f();
			fail("sct.i2f(); should not success");
		} catch (SQLException e) {
			System.out.println("OK, we expect: " + e.getMessage());
		}
		try {
			assertNull(sct.find("i2f"));
		} catch (SQLException e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		
		try {
			sct.i3f();
			fail("sct.i3f(); should not success");
		} catch (SQLException e) {
			System.out.println("OK, we expect: " + e.getMessage());
		}
		try {
			assertNull(sct.find("i3f"));
		} catch (SQLException e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		try {
			sct.i4();
			assertEquals("i4", sct.find("i4"));
			assertEquals("i42", sct.find("i42"));
		} catch (SQLException e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		try {
			sct.i5f();
			fail("sct.i5f(); should not success");
		} catch (Throwable e) {
//			e.printStackTrace();
			System.out.println("OK, we expect: " + e.getMessage());
		}
		try {
			assertNull(sct.find("i5f"));
		} catch (SQLException e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		System.out.println("cost : " + (System.currentTimeMillis() - start));
	}
	
	
	@Before
	public void setUp() throws Exception {
		asura = new Asura();
		asura.start(new MemoryComponentLoader(), new MemoryComponentLoader(WeaverService.class), new XmlComponentLoader(Thread.currentThread().getContextClassLoader().getResource("META-INF/extern/Atomikos-composite.xml")));
		asura.getConfigAdminService().load(new XmlMultipleConfigLoader("atomikos_h2_test_config.xml"));
	}

	@After
	public void tearDown() throws Exception {
		asura.shutdown();
	}

}
